package posecco.client;

import java.util.ArrayList;
import posecco.shared.*;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.gwtent.reflection.client.ClassType;
import com.gwtent.reflection.client.TypeOracle;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class UserList extends FlowPanel {

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final UserListServiceAsync userListService = GWT.create(UserListService.class);
	private final CellTable<User> table = new CellTable<User>();
	private final String[][] fields = {
			
			{"login"},
			{"firstName"},
			{"lastName"},			
			{"email"}	
	};
	
	public UserList() {
		
		RootPanel.get("content").clear();
		userListService.getUserList(new AsyncCallback<ArrayList<User>>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			public void onSuccess(ArrayList<User> result) {
				buildTable(result);
			}

		});
	}
	
	private void buildTable(ArrayList<User> data) {
	// Create a CellTable.
	table.getElement().addClassName("listTable");
	table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
	
	// Add a text column to show the name.
	for (int i = 0; i < fields.length; ++i) {
		
		addCol(fields[i][0]);
	}
	
	// Add a date column to show the birthday.
	/* DateCell dateCell = new DateCell();
	Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
	  @Override
	  public Date getValue(Contact object) {
	    return object.birthday;
	  }
	};
	table.addColumn(dateColumn, "Birthday"); */
	
	
	// Add a selection model to handle user selection.
	final SingleSelectionModel<User> selectionModel = new SingleSelectionModel<User>();
	table.setSelectionModel(selectionModel);
	selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
		public void onSelectionChange(SelectionChangeEvent event) {
			User selected = selectionModel.getSelectedObject();
		    if (selected != null) {
		    	Navigation.gotoPage("editUser" + selected.getId());
		    }
		}
	});
	
	// Set the total row count. This isn't strictly necessary, but it affects
	// paging calculations, so its good habit to keep the row count up to date.
	table.setRowCount(data.size(), true);
	
	// Push the data into the widget.
	table.setRowData(0, data);
	
	// Add it to the root panel.
		add(table);
	}
	
	private void addCol(String fieldName) {
		
		final String colLabel = Util.getMessage(fieldName + "Label");
		final String getterName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
		TextColumn<User> column = new TextColumn<User>() {
			@SuppressWarnings("rawtypes")
			@Override
			public String getValue(User object) {
				try {
					
					ClassType classType = TypeOracle.Instance.getClassType(User.class);
					return classType.invoke(object, getterName, (Object[])null).toString();
				}
				catch (Exception e) {
					
					e.printStackTrace();
					return null;
				}
			}
		};
		table.addColumn(column, colLabel);		
	}
}
